// 判断用户是否登录 - 调用函数
checkLogin()
// 读取并渲染用户名
renderUserName()
// 退出登录
logout()

// 获取学生统计数据
async function getData() {
  const { data } = await axios({
    url: '/dashboard',
  })
  // console.log('--学生统计数据', data);
  // 面板区域
  renderOverview(data.overview)
  // 学科薪资
  renderLineYearSalary(data.year)
  // 班级薪资分布
  renderPieClassSalary(data.salaryData)
  // 班级每组薪资
  renderBarGroupSalary(data.groupData)
  // 男女薪资分布
  renderPieGenderSalary(data.salaryData)
  // 籍贯分布
  renderProvince(data.provinceData)
}


function renderProvince(provinceData){
  console.log(provinceData);
  const dom = document.querySelector('#map')
  const myEchart = echarts.init(dom)
  const dataList = [
    { name: '南海诸岛', value: 0 },
    { name: '北京', value: 0 },
    { name: '天津', value: 0 },
    { name: '上海', value: 0 },
    { name: '重庆', value: 0 },
    { name: '河北', value: 0 },
    { name: '河南', value: 0 },
    { name: '云南', value: 0 },
    { name: '辽宁', value: 0 },
    { name: '黑龙江', value: 0 },
    { name: '湖南', value: 0 },
    { name: '安徽', value: 0 },
    { name: '山东', value: 0 },
    { name: '新疆', value: 0 },
    { name: '江苏', value: 0 },
    { name: '浙江', value: 0 },
    { name: '江西', value: 0 },
    { name: '湖北', value: 0 },
    { name: '广西', value: 0 },
    { name: '甘肃', value: 0 },
    { name: '山西', value: 0 },
    { name: '内蒙古', value: 0 },
    { name: '陕西', value: 0 },
    { name: '吉林', value: 0 },
    { name: '福建', value: 0 },
    { name: '贵州', value: 0 },
    { name: '广东', value: 0 },
    { name: '青海', value: 0 },
    { name: '西藏', value: 0 },
    { name: '四川', value: 0 },
    { name: '宁夏', value: 0 },
    { name: '海南', value: 0 },
    { name: '台湾', value: 0 },
    { name: '香港', value: 0 },
    { name: '澳门', value: 0 },
  ]
  // 1. 遍历准备好的假数据
  dataList.forEach(ele => {
    // console.log(ele);

    // 遍历的过程中, 真实的省份列表进行查找
    // (拿真实数据的每一项的name 和假数据的name做对比)
    // const res = provinceData.find(v => v.name === ele.name)  
    // 青海省 包含 青海
    // 数组 includes 判断是否包含某一项
    // 字符串 includes 判断是否包含某个字符串
    // v.name 真实数据 - 带省 或者 自治区...
    // ele.name 假数据 (插件要求的字段属性名) 
    // const res = provinceData.find(v => v.name.includes(ele.name))  
    // const res = provinceData.find(v => {
    //   return v.name.includes(ele.name)
    // })  

    const reg = /省|壮族自治区|自治区|维吾尔自治区|回族自治区/g

    const res = provinceData.find(v => {
      return v.name.replace(reg, '') === ele.name
    })  
    // console.log(res);
    // 找到了 有匹配的项
    // if (res !== undefined) {
    if (res) {
      ele.value = res.value
    }
  })


  const option = {
    title: {
      text: '籍贯分布',
      top: 10,
      left: 10,
      textStyle: {
        fontSize: 16,
      },
    },
    tooltip: {
      trigger: 'item',
      formatter: '{b}: {c} 位学员',
      borderColor: 'transparent',
      backgroundColor: 'rgba(0,0,0,0.5)',
      textStyle: {
        color: '#fff',
      },
    },
    visualMap: {
      min: 0,
      max: 6,
      left: 'left',
      bottom: '20',
      text: ['6', '0'],
      inRange: {
        color: ['#ffffff', '#0075F0'],
      },
      show: true,
      left: 40,
    },
    geo: {
      map: 'china',
      roam: false,
      zoom: 1.0,
      label: {
        normal: {
          show: true,
          fontSize: '10',
          color: 'rgba(0,0,0,0.7)',
        },
      },
      itemStyle: {
        normal: {
          borderColor: 'rgba(0, 0, 0, 0.2)',
          color: '#e0ffff',
        },
        emphasis: {
          areaColor: '#34D39A',
          shadowOffsetX: 0,
          shadowOffsetY: 0,
          shadowBlur: 20,
          borderWidth: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)',
        },
      },
    },
    series: [
      {
        name: '籍贯分布',
        type: 'map',
        geoIndex: 0,
        data: dataList,
      },
    ],
  }
  myEchart.setOption(option)
}



// 男女薪资分布
function renderPieGenderSalary(salaryData) {
  const myChart = echarts.init(document.querySelector('#gender'))
  const option = {
    title: [
      {
        text: '男女薪资分布',
        left: '12',
        top: '15',
        textStyle: {
          fontSize: 16
        }
      },
      {
        text: '男生',
        left: 'center',
        top: '45%',
        textStyle: {
          fontSize: 12
        }
      },
      {
        text: '女生',
        left: 'center',
        top: '85%',
        textStyle: {
          fontSize: 12
        }
      }

    ],
    tooltip: {
      trigger: 'item'
    },
    color: ['#fda224', '#5097ff', '#3abcfa', '#34d39a'],
    series: [
      {
        name: '男生薪资分布',
        type: 'pie',
        radius: ['20%', '30%'],
        center: ['50%', '30%'],
        data: salaryData.map(ele => {
          return {
            name: ele.label,
            value: ele.b_count
          }
        }),
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      },
      {
        name: '女生薪资分布',
        type: 'pie',
        radius: ['20%', '30%'],
        // 饼图中心坐标 参数一 水平方向 参数二 垂直方向
        center: ['50%', '70%'],
        data: salaryData.map(ele => {
          return {
            name: ele.label,
            value: ele.g_count
          }
        }),
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]
  };
  myChart.setOption(option)
}


// 班级每组薪资
function renderBarGroupSalary(groupData) {
  const myChart = echarts.init(document.querySelector('#lines'))
  const option = {
    xAxis: {
      type: 'category',
      axisLine: {
        lineStyle: {
          color: 'skyblue',
          type: 'dashed'
        }
      },
      // 刻度相关的设置
      axisLabel: {
        color: 'pink' // 文字颜色
      },
      data: groupData[1].map(ele => ele.name)
    },
    tooltip: {},
    yAxis: {
      type: 'value'
    },
    grid: {
      left: '70',
      top: '30',
      right: '50',
      bottom: '30'
    },
    series: [
      {
        data: groupData[1].map(ele => ele.hope_salary),
        type: 'bar',
        name: '期望薪资',
        itemStyle: {
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [{
              offset: 0, color: '#34D39A' // 0% 处的颜色
            }, {
              offset: 1, color: 'rgba(52,211,154,0.2)' // 100% 处的颜色
            }],
            global: false // 缺省为 false
          }
        }
      },
      {
        data: groupData[1].map(ele => ele.salary),
        type: 'bar',
        name: '实际薪资',
        itemStyle: {
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [{
              offset: 0, color: '#499FEE' // 0% 处的颜色
            }, {
              offset: 1, color: 'rgba(73,159,238,0.2)' // 100% 处的颜色
            }],
            global: false // 缺省为 false
          }
        }
      }
    ]
  };
  myChart.setOption(option)

  // 高亮切换
  const btns = document.querySelector('#btns')
  btns.addEventListener('click', function(e) {
      const target = e.target
      if (target.tagName === 'BUTTON') {
        // 排他思想
        document.querySelector('.btn-blue').classList.remove('btn-blue')
        target.classList.add('btn-blue')

        const text = target.innerHTML // 1 2 3 4 ...
        // 数据 - > 通过text 实现切换
        console.log(groupData[text]);
        // x轴数据的切换
        option.xAxis.data = groupData[text].map(ele => ele.name)
        option.series[0].data = groupData[text].map(ele => ele.hope_salary)
        option.series[1].data = groupData[text].map(ele => ele.salary)
        // 重新渲染 -> 数据变化 -> 视图才会更新
        myChart.setOption(option)

      }
  })
}





// 班级薪资分布
function renderPieClassSalary(salaryData) {
  const myChart = echarts.init(document.querySelector('#salary'))
  const option = {
    title: {
      text: '班级薪资分布',
      left: '12',
      top: '15'
    },
    tooltip: {
      trigger: 'item'
    },
    legend: {
      bottom: '5%',
      left: 'center'
    },
    color: ['#fda224', '#5097ff', '#3abcfa', '#34d39a'],
    series: [
      {
        name: '班级薪资分布',
        type: 'pie',
        // 半径 -> [内圆的半径, 外圆的半径]
        radius: ['55%', '70%'],
        avoidLabelOverlap: false,
        itemStyle: {
          borderRadius: 10,
          borderColor: '#fff',
          borderWidth: 2
        },
        label: {
          show: false,
          position: 'center'
        },
        emphasis: {
          label: {
            show: false,
            fontSize: 40,
            fontWeight: 'bold'
          }
        },
        labelLine: {
          show: false
        },
        data: salaryData.map(ele => {
          return {
            name: ele.label,
            value: ele.g_count + ele.b_count
          }
        })
        // data: [
        //   { value: 1048, name: 'Search Engine' },
        //   { value: 735, name: 'Direct' },
        //   { value: 580, name: 'Email' },
        //   { value: 484, name: 'Union Ads' },
        // ]
      }
    ]
  };
  myChart.setOption(option)
}


// 学科薪资走势
function renderLineYearSalary(year) {
  // 初始化echarts实例
  const myChart = echarts.init(document.querySelector('#line'))
  // 配置项
  const option = {
    title: {
      text: '2022全学科薪资走势',
      left: '12',
      top: '15'
    },
    tooltip: {
      trigger: 'axis'
    },
    xAxis: {
      type: 'category',
      // 坐标轴轴线相关设置
      axisLine: {
        lineStyle: {
          color: 'skyblue',
          type: 'dashed'
        }
      },
      data: year.map(ele => ele.month)
    },
    yAxis: {
      type: 'value',
      splitLine: {
        lineStyle: {
          type: 'dashed',
          color: 'teal'
        }
      }
    },
    grid: {
      top: '25%',
    },
    series: [
      {
        data: year.map(ele => ele.salary),
        type: 'line',
        symbol: 'emptyCircle',  // 图形/标记
        symbolSize: 10, // 标记大小
        lineStyle: {
          width: 6, // 线宽
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 1,
            y2: 0,
            colorStops: [{
              offset: 0, color: '#4a9eee' // 0% 处的颜色
            }, {
              offset: 1, color: '#5d76f0' // 100% 处的颜色
            }],
            global: false // 缺省为 false
          }
        },
        smooth: true,  // 平滑曲线
        areaStyle: {
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [{
              offset: 0, color: '#91c5f5' // 0% 处的颜色
            }, {
              offset: 1, color: 'rgba(255, 255, 255, 0)' // 100% 处的颜色
            }],
            global: false // 缺省为 false
          }
        } 
      }
    ]
  }
  // 实例和配置项进行关联
  myChart.setOption(option)
}


// 面板区域
function renderOverview(overview) {
  Object.keys(overview).forEach(ele => {
    document.querySelector(`.${ele}`).innerHTML = overview[ele]
  })
}


getData()